/*
 * 
 * 
 * 
 */
package com.hboxs.asl.entity;

import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.*;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Entity - 规格
 */
@Entity
@Table(name = "xx_specification")
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "xx_specification_sequence")
public class Specification extends OrderEntity {

    private static final long serialVersionUID = -6346775052811140926L;

    /**
     * 类型
     */
    public enum Type {

        /**
         * 文本
         */
        text,

        /**
         * 图片
         */
        image
    }

    /**
     * 名称
     */
    private String name;

    /**
     * 类型
     */
    private Type type;

    /**
     * 备注
     */
    private String memo;

    /**
     * 规格值
     */
    private List<SpecificationValue> specificationValues = new ArrayList<SpecificationValue>();

    /**
     * 商品
     */
    private Set<Product> products = new HashSet<Product>();

    /**
     * 获取名称
     *
     * @return 名称
     */
    @NotEmpty
    @Length(max = 200)
    @Column(nullable = false)
    public String getName() {
        return name;
    }

    /**
     * 设置名称
     *
     * @param name 名称
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取类型
     *
     * @return 类型
     */
    @NotNull
    @Column(nullable = false)
    public Type getType() {
        return type;
    }

    /**
     * 设置类型
     *
     * @param type 类型
     */
    public void setType(Type type) {
        this.type = type;
    }

    /**
     * 获取备注
     *
     * @return 备注
     */
    @Length(max = 200)
    public String getMemo() {
        return memo;
    }

    /**
     * 设置备注
     *
     * @param memo 备注
     */
    public void setMemo(String memo) {
        this.memo = memo;
    }

    /**
     * 获取规格值
     *
     * @return 规格值
     */
    @Valid
    @NotEmpty
    @OneToMany(mappedBy = "specification", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    @OrderBy("order asc")
    public List<SpecificationValue> getSpecificationValues() {
        return specificationValues;
    }

    /**
     * 设置规格值
     *
     * @param specificationValues 规格值
     */
    public void setSpecificationValues(List<SpecificationValue> specificationValues) {
        this.specificationValues = specificationValues;
    }

    /**
     * 获取商品
     *
     * @return 商品
     */
    @ManyToMany(mappedBy = "specifications", fetch = FetchType.LAZY)
    public Set<Product> getProducts() {
        return products;
    }

    /**
     * 设置商品
     *
     * @param products 商品
     */
    public void setProducts(Set<Product> products) {
        this.products = products;
    }

}